﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace UniversalUpdater
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Version InstalledVersion = null;
            string AppUpdateUrl = null;
            bool Silent = false;
            bool IsXML = false;
            // UniversalUpdater.exe 1.25.3333.48500 http://updateurl.cz/program.php?id=1d4e855fc
            if (args.Length > 0)
            {
                InstalledVersion = new Version(args[0]);
                AppUpdateUrl = args[1];
                Silent = args.Contains("s");
                IsXML = args.Contains("xml");
            }
            // uu.ini -> 1.25.3333.48500\nhttp://updateurl.cz/program.php?id=1d4e855fc
            else if (File.Exists("uu.ini"))
            {
                using (StreamReader sr = new StreamReader("uu.ini"))
                {
                    InstalledVersion = new Version(sr.ReadLine());
                    AppUpdateUrl = sr.ReadLine();
                    IsXML = !sr.EndOfStream && sr.ReadLine().ToLower() == "xml";
                }
            }
            else if (File.Exists("uu.xml"))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreWhitespace = true;
                settings.IgnoreComments = true;
                settings.ProhibitDtd = false;
                settings.XmlResolver = null;

                string element = "";
                using (XmlReader reader = XmlReader.Create("uu.xml", settings))
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                element = reader.Name;
                                break;

                            case XmlNodeType.Text:
                                if (element == "InstalledVersion") { InstalledVersion = new Version(reader.Value); }
                                if (element == "AppUpdateUrl") { AppUpdateUrl = reader.Value; }
                                if (element == "IsXML") { IsXML = bool.Parse(reader.Value); }
                                break;
                        }
                    }
                    reader.Close();
                }
            }
            else
            {
                MessageBox.Show("Params neither uu.ini/xml file NOT found!");
                return;
            }

            Application.Run(new UpdaterForm(InstalledVersion, AppUpdateUrl, Silent, IsXML));
        }
    }
}
